package com.penghaisoft.microwms.materialhand.web.outwarehouse.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.HandBaseController;
import com.penghaisoft.microwms.common.data.model.RawRequisitionSn;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.RawBackDto;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.service.IMesInterfaceService;
import com.penghaisoft.microwms.common.service.MesProperties;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.service.IMaterialHandOutWhService;

/**
 * @program: micro-wms
 * @description: 退料接口
 * @author: zhangx
 * @create: 2018-05-17 15:21
 **/
@RestController
@RequestMapping("outwh/backmat")
public class BackMaterialController extends HandBaseController {

    @Autowired
    private IMaterialHandOutWhService materialHandOutWhService;
    
    @Resource
	private MesProperties mesProperties;
    
    @Autowired
	private IMesInterfaceService mesInterfaceService;
    
    /**
     * 扫描条码
     * @param rawBackDto
     * @return
     */
    @PostMapping("scanOld")   //这个不用了
    public HandResp scanBarcode(@RequestBody RawBackDto rawBackDto){
        HandResp result = new HandResp();
        result.setCode(Constant.SUCCESS);
        if (rawBackDto.getBarcode()==null || rawBackDto.getBarcode().equals("")){
            result.setCode(Constant.FAIL);
            result.setMsg("请输入条码！");
            return result;
        }
//        检查是否符合退料
        TransResult resp = materialHandOutWhService.backMatScanBarCheck(rawBackDto);
        if(resp != null && Constant.FAIL.equals(resp.getCode())){
        	 result.setCode(Constant.FAIL);
             result.setMsg(resp.getMsg());
             return result;
        }
        
        String url = mesProperties.getUrl() + "/imes-provider/productplan/follow/ordersplit/back";
        
        RawRequisitionSn rawRequisitionSn = (RawRequisitionSn)resp.getData();
        
        JSONObject ob = new JSONObject();
        ob.put("requisitionNo", rawRequisitionSn.getRequisitionNo());
        ob.put("requisitionType", rawRequisitionSn.getRequisitionType());
        ob.put("model", rawRequisitionSn.getModel());
        ob.put("modelType", rawRequisitionSn.getModelType());
        ob.put("barcode", rawRequisitionSn.getBarcode());
        ob.put("backAmount", rawRequisitionSn.getBackAmount());
//		调mes新增接口
		HandResp handResp = mesInterfaceService.publicWSResp(url, MediaType.APPLICATION_JSON_TYPE, ob.toJSONString());
		if(handResp.getCode() != null && handResp.getCode().equals("0")){
			result.setCode(handResp.getCode());
			result.setMsg(handResp.getMsg());
	        return result;
		}else{
			resp = materialHandOutWhService.backMatScanBar(rawBackDto);
	        
	        if (resp.getCode().equals(Constant.FAIL)){
	        	result.setCode(Constant.FAIL);
	            result.setMsg(resp.getMsg());
	        }else {
	            List list = new ArrayList<>();
	            list.add(resp.getData());
	            result.setList(list);
	            result.setValue(resp.getMsg());
	        }
	        return result;
		}
    }
    
    
    /**
     * 扫描条码
     * @param rawBackDto
     * @return
     */
    @PostMapping("scan")   //现在用这个
    public HandResp scanBarcodeNew(@RequestBody RawBackDto rawBackDto){
        HandResp result = new HandResp();
        result.setCode(Constant.SUCCESS);
        if (rawBackDto.getBarcode()==null || rawBackDto.getBarcode().equals("")){
            result.setCode(Constant.FAIL);
            result.setMsg("请输入条码！");
            return result;
        }
        
        //检查是否为手工出库的退料方式
        TransResult isOrNotHandOut = materialHandOutWhService.isOrNotHandOut(rawBackDto);
        if(isOrNotHandOut != null && Constant.FAIL.equals(isOrNotHandOut.getCode())){
        	 result.setCode(Constant.FAIL);
             result.setMsg(isOrNotHandOut.getMsg());
             return result;
        }else if(isOrNotHandOut != null && "Y".equals(isOrNotHandOut.getMsg())){
        	//先判断是否符合手工出库的退料
        	//然后进行手工出库退料
        	TransResult resp = materialHandOutWhService.backHandOutCheck(rawBackDto);
        	if (resp != null && resp.getCode().equals(Constant.FAIL)){
  	        	result.setCode(Constant.FAIL);
  	            result.setMsg(resp.getMsg());
  	        }else {
  	            List list = new ArrayList<>();
  	            list.add(resp.getData());
  	            result.setList(list);
  	            result.setValue(resp.getMsg());
  	        }
  	        return result;
        }else{
//          检查是否符合退料
          TransResult resp = materialHandOutWhService.backMatScanBarCheck(rawBackDto);
          if(resp != null && Constant.FAIL.equals(resp.getCode())){
          	 result.setCode(Constant.FAIL);
               result.setMsg(resp.getMsg());
               return result;
          }
          
          String url = mesProperties.getUrl() + "/imes-provider/productplan/follow/ordersplit/back";
          
          RawRequisitionSn rawRequisitionSn = (RawRequisitionSn)resp.getData();
          
          JSONObject ob = new JSONObject();
          ob.put("requisitionNo", rawRequisitionSn.getRequisitionNo());
          ob.put("requisitionType", rawRequisitionSn.getRequisitionType());
          ob.put("model", rawRequisitionSn.getModel());
          ob.put("modelType", rawRequisitionSn.getModelType());
          ob.put("barcode", rawRequisitionSn.getBarcode());
          ob.put("backAmount", rawRequisitionSn.getBackAmount());
//  		调mes新增接口
  		HandResp handResp = mesInterfaceService.publicWSResp(url, MediaType.APPLICATION_JSON_TYPE, ob.toJSONString());
  		if(handResp.getCode() != null && handResp.getCode().equals("0")){
  			result.setCode(handResp.getCode());
  			result.setMsg(handResp.getMsg());
  	        return result;
  		}else{
  			resp = materialHandOutWhService.backMatScanBar(rawBackDto);
  	        
  	        if (resp.getCode().equals(Constant.FAIL)){
  	        	result.setCode(Constant.FAIL);
  	            result.setMsg(resp.getMsg());
  	        }else {
  	            List list = new ArrayList<>();
  	            list.add(resp.getData());
  	            result.setList(list);
  	            result.setValue(resp.getMsg());
  	        }
  	        return result;
  		}
        }
        
        
    }
}
